<template>
  <div class="app-container">
    <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
    >
      <el-form-item label="姓名" prop="schoolName">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入姓名"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="联系电话" prop="phone">
        <el-input
            v-model="queryParams.phone"
            placeholder="请输入联系电话"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="身份证号" prop="idNumber">
        <el-input
            v-model="queryParams.idNumber"
            placeholder="请输入身份证号"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <!--      <el-form-item label="学生姓名" prop="name">-->
      <!--        <el-input-->
      <!--            v-model="queryParams.name"-->
      <!--            placeholder="请输入学生姓名"-->
      <!--            clearable-->
      <!--            @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="年级" prop="grade">-->
      <!--        <el-input-->
      <!--            v-model="queryParams.grade"-->
      <!--            placeholder="请输入年级"-->
      <!--            clearable-->
      <!--            @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->

      <!--      <el-form-item label="籍贯" prop="nativePlace">-->
      <!--        <el-input-->
      <!--            v-model="queryParams.nativePlace"-->
      <!--            placeholder="请输入籍贯"-->
      <!--            clearable-->
      <!--            @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="家庭住址" prop="homeAddress">-->
      <!--        <el-input-->
      <!--            v-model="queryParams.homeAddress"-->
      <!--            placeholder="请输入家庭住址"-->
      <!--            clearable-->
      <!--            @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="联系电话" prop="phone">-->
      <!--        <el-input-->
      <!--            v-model="queryParams.phone"-->
      <!--            placeholder="请输入联系电话"-->
      <!--            clearable-->
      <!--            @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="家庭成员姓名" prop="homeName" label-width="110px">-->
      <!--        <el-input-->
      <!--            v-model="queryParams.homeName"-->
      <!--            placeholder="请输入家庭成员姓名"-->
      <!--            clearable-->
      <!--            @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="家庭成员联系电话" prop="homePhone" label-width="110px">-->
      <!--        <el-input-->
      <!--            v-model="queryParams.homePhone"-->
      <!--            placeholder="请输入家庭成员联系电话"-->
      <!--            clearable-->
      <!--            @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button
            type="primary"
            icon="search"
            size="mini"
            @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button icon="refresh" size="mini" @click="resetQuery"
        >重置
        </el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

    </el-row>

    <el-table
        v-loading="loading"
        :data="informationList"
        @selection-change="handleSelectionChange"
    >
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <!--  1.教师表 2.学生表 3.网签表 4.权利人-->
      <el-table-column label="数据类型" align="center" width="150">
        <template #default="{ row }">
          <el-tag :type=getTypeObj(row.type).color>{{ getTypeObj(row.type).value }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="name" width="250"/>
      <el-table-column label="身份证号" align="center" prop="idNumber" width="450"/>
      <el-table-column label="联系电话" align="center" prop="phone"/>
      <!--      <el-table-column label="学校名称" align="center" prop="schoolName"/>-->
      <!--      <el-table-column label="籍贯" align="center" prop="nativePlace"/>-->
      <!--      <el-table-column label="现住址" align="center" prop="address"/>-->
      <!--      <el-table-column label="家庭成员姓名" align="center" prop="homeName"/>-->
      <!--      <el-table-column label="家庭成员联系电话" align="center" prop="homePhone"/>-->
      <!--      <el-table-column label="性别" align="center" prop="sex"/>-->
      <!--      <el-table-column label="规划用途" align="center" prop="programme"/>-->
      <!--      <el-table-column label="签订时间" align="center" prop="signDate"/>-->
      <!--      <el-table-column label="购房地址" align="center" prop="buyHomeAddress"/>-->
      <!--      <el-table-column label="年级" align="center" prop="grade"/>-->
      <!--      <el-table-column label="登记时间" align="center" prop="registDate"/>-->

      <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
              size="mini"
              type="text"
              @click="showDetail(scope.row)"
          >查看详情
          </el-button
          >
        </template>
      </el-table-column>

    </el-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <el-dialog
        v-model="open"
        :title=title
        width="60%"
        :before-close="handleClose"

    >
      <div class="detail-container">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="姓名" v-if="getIsVisible('name')">
            {{ detail.name }}
          </el-descriptions-item>

          <el-descriptions-item label="现住址" v-if="getIsVisible('address')">
            {{ detail.address }}
          </el-descriptions-item>

          <el-descriptions-item label="身份证号码" v-if="getIsVisible('idNumber')">
            {{ detail.idNumber }}
          </el-descriptions-item>

          <el-descriptions-item label="联系电话" v-if="getIsVisible('phone')">
            {{ detail.phone }}
          </el-descriptions-item>

          <el-descriptions-item label="性别" v-if="getIsVisible('sex')">
            {{ detail.sex }}
          </el-descriptions-item>

          <el-descriptions-item label="学校名称" v-if="getIsVisible('schoolName')">
            {{ detail.schoolName }}
          </el-descriptions-item>

          <el-descriptions-item label="年级" v-if="getIsVisible('grade')">
            {{ detail.grade }}
          </el-descriptions-item>

          <el-descriptions-item label="籍贯" v-if="getIsVisible('nativePlace')">
            {{ detail.nativePlace }}
          </el-descriptions-item>

          <el-descriptions-item label="家庭成员姓名" v-if="getIsVisible('homeName')">
            {{ detail.homeName }}
          </el-descriptions-item>

          <el-descriptions-item label="家庭成员联系电话" v-if="getIsVisible('homePhone')">
            {{ detail.homePhone }}
          </el-descriptions-item>

          <el-descriptions-item label="规划用途" v-if="getIsVisible('programme')">
            {{ detail.programme }}
          </el-descriptions-item>

          <el-descriptions-item label="购房地址" v-if="getIsVisible('buyHomeAddress')">
            {{ detail.buyHomeAddress }}
          </el-descriptions-item>

          <el-descriptions-item label="签订时间" v-if="getIsVisible('signDate')">
            {{ detail.signDate }}
          </el-descriptions-item>

          <el-descriptions-item label="登记时间" v-if="getIsVisible('registDate')">
            {{ detail.registDate }}
          </el-descriptions-item>
        </el-descriptions>
        <div v-show="hasTab" class="tab-list">
          <hr/>
          <span>网签列表</span>
          <el-table
              v-loading="loading"
              :data="detail.peTabInformationList"
              @selection-change="handleSelectionChange"
          >

            <!--      <el-table-column label="序号" align="center" prop="id" />-->
            <el-table-column label="房屋地址" align="center" prop="homeAddress"/>
            <el-table-column label="购房人" align="center" prop="homebuyer"/>
            <el-table-column label="身份证号" align="center" prop="idNumber"/>
            <el-table-column
                label="购房人住址"
                align="center"
                prop="homebuyerAddress"
            />
            <el-table-column label="性别" align="center" prop="sex"/>
            <el-table-column label="规划用途" align="center" prop="programme"/>
            <el-table-column
                label="签订时间"
                align="center"
                prop="signDate"
                width="180"
            >

            </el-table-column>

          </el-table>
        </div>
      </div>

      <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">关闭</el-button>

      </span>
      </template>
    </el-dialog>

  </div>
</template>

<script>
import {
  selectList
} from "@/api/person/search.js";

export default {
  name: "search",
  data() {
    return {
      typeArray: [{key: 1, value: "教师", color: 'success'},
        {key: 2, value: "学生", color: 'info'},
        {key: 3, value: "网签", color: 'warning'},
        {key: 4, value: "权利人", color: 'danger'},
      ],
      fieldMapArray: [
        {
          key: 1,
          fieldList: ["schoolName", 'name', 'idNumber', "nativePlace", "address", "phone", "homeName", "homePhone"]
        },
        {
          key: 2,
          fieldList: ["schoolName", 'name', 'grade', 'idNumber', 'nativePlace', 'address', 'phone', "homeName", "homePhone"]
        },
        {key: 3, fieldList: ['address', 'name', 'idNumber', 'buyHomeAddress', 'sex', 'programme', 'signDate']},
        {key: 4, fieldList: ['name', 'idNumber', 'phone', 'address', 'registDate']},
      ],
      fieldArray: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      informationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否有网签数据
      hasTab: false,
      // 查询参数
      queryParams: {

        pageNum: 1,
        pageSize: 10,
        schoolName: null,
        name: null,
        grade: null,
        idNumber: null,
        nativePlace: null,
        homeAddress: null,
        phone: null,
        homeName: null,
        programme: null,
        buyHomeAddress: null,
        sex: null,
        address: null,
        /** 签订时间 */
        signDate: null,
        /**
         * 登记时间
         */
        registDate: null,
      },
      // 表单参数
      form: {},
      // 子组件数据
      detail: {},
      // 表单校验
      rules: {
        idNumber: [
          {required: true, message: '请输入身份证号', trigger: 'blur'},
          {validator: this.validateIdCard, trigger: 'blur'}
        ]
      },
    };
  },
  created() {

    this.getList();
    this.openMessages();
  },
  methods: {
    // 根据不同的表 展示不同的内容
    getIsVisible(filed) {
      return this.fieldArray.includes(filed);
    },
    handleClose() {
      this.open = false;
      this.hasTab = true;

    },
    openMessages() {
      this.$message({
        message: '请输入查询数据进行全局查询',
        type: 'success'
      });

    },
    // 获取对象
    getTypeObj(type) {
      const obj = this.typeArray.filter(item => item.key === type);
      return obj[0];
    },

    // 身份证校验规则
    validateIdCard(rule, value, callback) {
      if (!value) {
        callback(new Error('请输入身份证号'))
        return
      }

      // 身份证号码正则
      const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/

      if (!reg.test(value)) {
        callback(new Error('身份证号码格式不正确'))
        return
      }

      callback()
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      selectList(this.queryParams).then((response) => {
        this.informationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        schoolName: null,
        name: null,
        grade: null,
        idNumber: null,
        nativePlace: null,
        homeAddress: null,
        phone: null,
        homeName: null,
        homePhone: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      // this.queryParams.name = '莫文慧';
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    showDetail(scop) {
      this.hasTab = false;
      this.title = this.getTypeObj(scop.type).value;
      this.detail = scop;
      // 初始化字段信息
      const obj = this.fieldMapArray.filter(item => item.key === scop.type);
      this.fieldArray = obj[0].fieldList;
      if (scop.type === 1) {
        this.hasTab = true;
      }
      this.open = true;

    }
  },
};
</script>

<style lang="scss">
.tab-list {
  height: 300px;
  overflow: auto; /* 自动显示滚动条 */
  margin-top: 20px;
  text-align: center;

  span {
    font-size: 20px;
    margin-bottom: 10px;
  }
}
</style>
